//
//  MyCollectViewController.m
//  DaJiang
//
//  Created by sally on 16/10/9.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "ExpertCell.h"
#import "ExpertModel.h"
#import "MyCollectViewController.h"

@interface MyCollectViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *tableData;
@property (assign, nonatomic) NSInteger pager;

@end

@implementation MyCollectViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self initControl];
  [self initData];
  [self setRefresh];
}

#pragma mark 初始化控件
- (void)initControl {
  self.title = @"我的心愿单";
  [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

#pragma mark 初始化数据
- (void)initData {
  self.tableData = [NSMutableArray array];
}

- (void)setRefresh {
  __weak typeof(self) weakSelf = self;
  // 添加下拉刷新列表数据功能
  _tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:weakSelf refreshingAction:@selector(loadRefreshData)];

  // 添加上拉加载更多列表数据功能
  _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:weakSelf refreshingAction:@selector(loadMoreData)];

  // 触发刷新
  [_tableView.header beginRefreshing];
}

#pragma mark - 获取数据
- (void)loadRefreshData {
  _pager = 1;
  [self loadData];
}

- (void)loadMoreData {
  _pager = _pager + 1;
  [self loadData];
}

- (void)loadData {
  self.tableView.footer.hidden = NO;
  NSInteger cursor = _pager > 1 ? [[self.tableData lastObject] commentid] : GCursorInit;

  [CollectModel queryCollectListWithCursor:cursor completion:^(NSMutableArray *collectList) {
    if (collectList) {
      // 下拉刷新数据
      if (_pager == 1) {
        [self.tableData removeAllObjects];
        [self.tableData addObjectsFromArray:collectList];
        [self.tableView reloadData];

        if (collectList.count < GListCount) {
          self.tableView.footer.hidden = YES;
        }
      }
      // 上拉加载更多
      else {
        [self.tableData addObjectsFromArray:collectList];
        [self.tableView reloadData];
        if (collectList.count < GListCount) {
          self.tableView.footer.hidden = YES;
        }
      }

    } else {
      NSError *error = [CommentModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }

    [self.tableView.header endRefreshing];
    [self.tableView.footer endRefreshing];
  }];
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return self.tableData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [[[self.tableData objectAtIndex:section]pro_topics]count] + 1;//专家信息+话题
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return 0.1; //section头部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  return GNormalMargin; //section底部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  if (indexPath.row == 0) { //专家信息
    return GMineHeadCellHeight;
  }
  return GSmallViewHeight; //话题信息
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  if (indexPath.row == 0) { //专家信息
    ExpertCell *cell = (ExpertCell *) [tableView dequeueReusableCellWithIdentifier:@"ExpertCell"];
    if (!cell) {
      cell = [[[NSBundle mainBundle] loadNibNamed:@"ExpertCell" owner:nil options:nil] lastObject];
    }

    //列表项点击性
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    
    cell.cellData = [[self.tableData objectAtIndex:indexPath.section]pro_user];
    return cell;
    
  } else { //话题信息
    //主副标题形式
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];
    NSMutableArray *topicArr = [[self.tableData objectAtIndex:indexPath.section]pro_topics];
    TopicModel *topicModel = [topicArr objectAtIndex:indexPath.row - 1];
    
    //主标题
    cell.textLabel.font = GFontWithSize(GMiddleFontSize);
    cell.textLabel.textColor = GBlackColor;
    cell.textLabel.text = topicModel.data.topic_name;

    //副标题
    cell.detailTextLabel.font = GFontWithSize(GMiddleFontSize);
    cell.detailTextLabel.textColor = GOrangeColor;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"¥%.2f",topicModel.data.topic_price];

    //列表项点击性
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
  }
}

#pragma mark TableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES]; //返回后选中状态消失
  UserModel *userModel = [[self.tableData objectAtIndex:indexPath.section]pro_user];
  [OperationManager gotoExpertDetailVcWithId:userModel.uid expertName:userModel.user_data.user_name];
}

@end
